<?php

namespace App\Services\MakeProductOrder;

use Carbon\Carbon;
use App\Models\Order;
use App\Models\Address;
use App\Models\Cart;
use App\Models\Product;
use App\Http\Controllers\Api\V1\Requests\FormRequest;

class MakeProductOrderRequest extends FormRequest
{
	protected ?Address $address = null;

	public function rules(): array
	{
		/*$todayUnpaidOrders = $this->user()->todayUnpaidOrdersCount();
		if ($todayUnpaidOrders >= 10) {
			$this->user()->update(['is_locked' => true]);
			fail('账号操作异常，请联系客服');
		}*/

		return [
			'product_id' => [
				'required','integer', $this->product()
			]		];
	}


	public function product()
	{
		return function ($attribute, $value, $fail) {
			if (!$product = Product::find($value)) {
				return fail('此商品不存在');
			} elseif (!$product->is_sale) {
				return fail('此商品未上架');
			} elseif ($product->stock === 0) {
				return fail('此商品已售完');
			} elseif (big_decimal($product->price_vip)->isGreaterThan($this->user()->balance)) {
				return fail('用户积分不足');
			}
			return true;
		};
	}

	
}
